package com.gamificationlibrary.gamificationlibrary.leaderboard;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.util.Log;

import com.gamificationlibrary.gamificationlibrary.model.Area;
import com.gamificationlibrary.gamificationlibrary.model.Player;
import com.gamificationlibrary.gamificationlibrary.model.Score;




public class Leaderboard implements Serializable {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7121176783693697712L;
	public static int ALLTIME=0;
	public static int DAILY=1;
	public static int WEEKLY=7;
	public static String ALLCOUNTRY="AllCountry";
	public static String EVERYBODY="Everybody";
	public static String FRIENDS="Friends";
	public static String SORTBYASCENDING="Ascending";
	public static String SORTBYDESCENDING="Descending";
	
	private int numDays;
	private String people;
	private Area area;
	private ArrayList<LeaderboardRow> playerList;
	
	public Leaderboard(String people,int numDays,Area area)
	{
		this.setPeople(people);	
		this.setNumDays(numDays);
		this.setArea(area);
		playerList=new ArrayList<LeaderboardRow>();
	}
	
	public Leaderboard(String people)
	{
		this.setPeople(people);	
		this.setNumDays(ALLTIME);
		Area area=new Area();
		area.setName(ALLCOUNTRY);
		this.setArea(area);
		playerList=new ArrayList<LeaderboardRow>();
	}
	
	public void populate(ArrayList<Player> p)
	{
		for(int i=0;i<p.size();i++)
 		{
			
 			Player player=p.get(i);
			if(numDays==this.ALLTIME)
	 			{
	 				if(area.getName().equals(this.ALLCOUNTRY))
	 				{
			 			playerList.add(new LeaderboardRow(player,player.getScoreTotal()));
			 		}
	 				else  playerList.add(new LeaderboardRow(player,player.getGeographicalScore(area)));
	 			}
 			else
 			{
 				if(area.getName().equals(this.ALLCOUNTRY))
 				{
 					playerList.add(new LeaderboardRow(player,player.getTemporalScore(numDays)));
			 		
		 		}
 				else  playerList.add(new LeaderboardRow(player,player.getGeoTemporalScore(numDays, area)));
 			}
 		}
		
	
		sortLeaderboard_byDescending();
	}
	
	private void computeRanks()
	{
		for(int i=0;i<playerList.size();i++)
		{
			playerList.get(i).setRank(i+1);
		}
	}
	
	public int getRankPlayer(Player p)
	{
		for(int i=0;i<playerList.size();i++)
		{
			if(playerList.get(i).getPlayer().getIdentification().equals(p.getIdentification()))
			{
				return playerList.get(i).getRank();
			}
		}
		return 0;
	}
	
	public Score getScorePlayer(Player p)
	{
		
		for(int i=0;i<playerList.size();i++)
		{
			if(playerList.get(i).getPlayer().getIdentification().equals(p.getIdentification()))
			{
				return playerList.get(i).getScore();
			}
		}
		return null;
	}

	public int getNumDays() {
		return numDays;
	}

	public void setNumDays(int numDays) {
		this.numDays = numDays;
	}

	public ArrayList<LeaderboardRow> getPlayerList() {
		return playerList;
	}

	public String getPeople() {
		return people;
	}

	public void setPeople(String people) {
		this.people = people;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

		
	
public void sortLeaderboard_byAscending()
	{
		Collections.sort(playerList, new Comparator<LeaderboardRow>() {
			@Override
			public int compare(LeaderboardRow lhs,
					LeaderboardRow rhs) {
				// TODO Auto-generated method stub
				   Integer scoreP1 = (Integer) lhs.getScore().getValue();
				   Integer scoreP2 = (Integer) rhs.getScore().getValue();
				   int compare=scoreP1.compareTo(scoreP2);
				   if(compare==0)
				   {
					   String nameP1=(String) lhs.getPlayer().getIdentification();
					   String nameP2=(String) rhs.getPlayer().getIdentification();
					   
					   return nameP1.compareTo(nameP2);
					   
				   }
				   else 
					   return compare;
			}
	    });
		
		computeRanks();
	}

		public void sortLeaderboard_byDescending()
		{
			Collections.sort(playerList, new Comparator<LeaderboardRow>() {
				@Override
				public int compare(LeaderboardRow lhs,
						LeaderboardRow rhs) {
					// TODO Auto-generated method stub
					   Integer scoreP1 = (Integer) lhs.getScore().getValue();
					   Integer scoreP2 = (Integer) rhs.getScore().getValue();
					   int compare=scoreP2.compareTo(scoreP1);
					   if(compare==0)
					   {
						   String nameP1=(String) lhs.getPlayer().getIdentification();
						   String nameP2=(String) rhs.getPlayer().getIdentification();
						   
						   return nameP1.compareTo(nameP2);
						   
					   }
					   else 
						   return compare;
				}
		    });
			
			computeRanks();
		}


		/*public void setSort(String lsort) {
			switch (lsort) {
			
			case Leaderboard.SORTBYASCENDING:			
				this.sort=Leaderboard.SORTBYASCENDING;
				break;
				
			case Leaderboard.SORTBYDESCENDING:			
				this.sort=Leaderboard.SORTBYDESCENDING;
				break;	
				
			default:
				this.sort=Leaderboard.SORTBYDESCENDING;
		      break;
		}*/
		
}
